package com.micro.commonlibrary.tools;

import android.content.Context;
import android.content.SharedPreferences;

//sp
public class SPTools {

    private static SharedPreferences mSharedPreferences;

    private SPTools() {

    }

    private final static String SHARED_NAME = "common";

    public static void putString(String key, String text) {
        spIsNull();
        mSharedPreferences.edit().putString(key, text).apply();
    }

    //这个是判断为空
    private static void spIsNull() {
        if (mSharedPreferences == null)
            mSharedPreferences = UiTools.getContext().getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
    }

    public static void putBoolean(String key, boolean b) {
        spIsNull();
        mSharedPreferences.edit().putBoolean(key, b).apply();
    }

    public static void putInt(String key, int i) {
        spIsNull();
        mSharedPreferences.edit().putInt(key, i).apply();
    }

    public static void clearSP() {
        spIsNull();
        mSharedPreferences.edit().clear().apply();
    }

    public static String getString(String key, String def) {
        spIsNull();
        return mSharedPreferences.getString(key, def);
    }

    public static int getInt(String key, int def) {
        spIsNull();
        return mSharedPreferences.getInt(key, def);
    }

    public static boolean getBoolean(String key, boolean def) {
        spIsNull();
        return mSharedPreferences.getBoolean(key, def);
    }
}
